//
//  PhotoBrowserCollectionViewCell.swift
//  XDweibo
//
//  Created by HanXingda on 15/7/5.
//  Copyright © 2015年 韩兴达. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

protocol PhotoBrowserCollectionViewCellDelegate: NSObjectProtocol {
    func didSelctedCurrentImage()
}
class PhotoBrowserCollectionViewCell: UICollectionViewCell,UIScrollViewDelegate {
    weak var photoDelegate : PhotoBrowserCollectionViewCellDelegate?
    var imageURLs: NSURL? {
        didSet {
            //重设偏移属性
            resetScrollView()
            //转菊花
            indicator.startAnimating()
            imageView.sd_setImageWithURL(imageURLs, placeholderImage: nil) { (image, error, _, _) in
                
               self.imageViewPosition(image)
               //隐藏菊花
               self.indicator.stopAnimating()
            }
        }
    }
    //点击图片
    func clickImage() {
        photoDelegate?.didSelctedCurrentImage()
    }
    
    // MARK: - UIScrollViewDelegate
    // 告诉scrollview缩放谁
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    //重新计算边距
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        var offSetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offSetX < 0 {
            offSetX = 0
        }
        var offSetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offSetY < 0 {
            offSetY = 0
        }
        scrollView.contentInset = UIEdgeInsets(top: offSetY, left: offSetX, bottom: 0, right: 0)
    }
    //重新设定scrollview偏移属性
    private func resetScrollView() {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentSize = CGSizeZero
        scrollView.contentOffset = CGPointZero
    }
    //重新计算imageView的CGSize
    private func displaySize(image: UIImage) -> CGSize {
        let scale = image.size.height / image.size.width
        return CGSize(width: bounds.width, height: bounds.width * scale)
    }
    //计算imageView的位置frame
    private func imageViewPosition(image: UIImage) {
        let size = displaySize(image)
        
        //长图
        if size.height > bounds.height {
            scrollView.contentInset = UIEdgeInsetsZero
            scrollView.contentSize = size
            imageView.frame = CGRect(origin: CGPointZero, size: size)
        } else {
            //短图
            let top = (bounds.height - size.height) * 0.5
            scrollView.contentInset = UIEdgeInsets(top: top, left: 0, bottom: 0, right: 0)
            imageView.frame = CGRect(origin: CGPointZero, size: size)
        
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        scrollView.addSubview(imageView)
        addSubview(indicator)
        
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: scrollView, size: nil)
        
        scrollView.frame = bounds
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
        //添加手势
        let tap = UITapGestureRecognizer(target: self, action: "clickImage")
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //懒加载
    lazy var scrollView = UIScrollView()
    lazy var imageView = UIImageView()
    //菊花
    lazy var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
}
